package com.shigu.paypwd.service;

import com.opentae.data.mall.beans.MallWallet;
import com.opentae.data.mall.beans.ManagerMallUser;
import com.opentae.data.mall.interfaces.MallOrderMapper;
import com.opentae.data.mall.interfaces.MallWalletMapper;
import com.opentae.data.mall.interfaces.ManagerMallUserMapper;
import com.shigu.MallResponse;
import com.shigu.exceptions.WapException;
import com.shigu.paypwd.util.MD5Util;
import com.shigu.paypwd.vo.HasPwdResponse;
import com.shigu.paypwd.vo.MoneyResponse;
import com.shigu.paypwd.vo.PersonCenterResponse;
import com.shigu.phoneCode.enums.PhoneCodeType;
import com.shigu.phoneCode.service.PhoneCodeService;
import com.shigu.trade.enums.TradeListStaus;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class PwdService {
    @Autowired
    ManagerMallUserMapper managerMallUserMapper;
    @Autowired
    MallOrderMapper mallOrderMapper;
    @Autowired
    PhoneCodeService phoneCodeService;
    @Autowired
    MallWalletMapper mallWalletMapper;

    /**
     * @description 设置密码
     * @author 郭尔
     * @date 2019/1/13 16:38
     */
    public MallResponse setPayPwd(Long userId, String payPwd) {
        ManagerMallUser user = new ManagerMallUser();
        user.setMallUserId(userId);
        user.setPayPwd(MD5Util.md5(payPwd));
        managerMallUserMapper.updateByPrimaryKeySelective(user);
        return new MallResponse().success();
    }

    /**
     * @description 判断是否有密码
     * @author 郭尔
     * @date 2019/1/17 10:56
     */
    public HasPwdResponse hasPayPwd(Long userId) {
        ManagerMallUser user = managerMallUserMapper.selectByPrimaryKey(userId);
        return new HasPwdResponse(StringUtils.isNotBlank(user.getPayPwd())).success();
    }

    /**
     * @description 个人中心信息
     * @author 郭尔
     * @date 2019/1/17 11:06
     */
    public PersonCenterResponse personCenter(Long userId) {
        PersonCenterResponse response = new PersonCenterResponse();
        ManagerMallUser user = managerMallUserMapper.selectByPrimaryKey(userId);

        response.setHasPwd(StringUtils.isNotBlank(user.getPayPwd()));
        response.setWaitPay(mallOrderMapper.countOrderList(userId, TradeListStaus.WAITPAY.getValue()));
        ;
        response.setWaitReceive(mallOrderMapper.countOrderList(userId, TradeListStaus.WAITRECEIVE.getValue()));
        response.setWaitSend(mallOrderMapper.countOrderList(userId, TradeListStaus.WAITSEND.getValue()));
        response.setAllOrder(mallOrderMapper.countOrderList(userId, TradeListStaus.ALLOrder.getValue()));


        MallWallet wallet = mallWalletMapper.selByUserId(userId);
        response.setWalletMoney(wallet.getMallMoney().toString());
        response.setCostMoney(user.getHaveCost().toString());


        return response.success();
    }


    /**
     * @description 验证老密码
     * @author 郭尔
     * @date 2019/1/17 13:30
     */
    public MallResponse checkOldPwd(Long userId, String oldPwd) throws WapException {
        ManagerMallUser user = managerMallUserMapper.selectByPrimaryKey(userId);
        if (!MD5Util.md5(oldPwd).equals(user.getPayPwd())) {
            throw new WapException("旧密码错误");
        }
        return new MallResponse().success();
    }

    /**
     * @description 查询用户余额
     * @author 郭尔
     * @date 2019/1/22 14:01
     */
    public MoneyResponse walletMoney(Long userId) {
        MoneyResponse response = new MoneyResponse();

        MallWallet wallet = mallWalletMapper.selByUserId(userId);
        if(wallet != null){
            response.setMoney(wallet.getMallMoney()+"");
            response.setVoucher(wallet.getVoucher()+"");
        }
        return response.success();
    }

    /**
     * @description 修改登录密码
     * @author 郭尔
     * @date 2019/2/14 17:45
     */
    public MallResponse changeLoginPwd(Long userId, String oldPassword, String newPassword) throws WapException {
        ManagerMallUser user = managerMallUserMapper.selectByPrimaryKey(userId);
        if (!user.getPassword().equals(MD5Util.md5(oldPassword))) {
            throw new WapException("旧密码错误");
        }
        user.setPassword(MD5Util.md5(newPassword));
        managerMallUserMapper.updateByPrimaryKeySelective(user);

        return new MallResponse().success();
    }

    /**
     * @description 修改用户名
     * @author 郭尔
     * @date 2019/2/14 17:55
     */
    public MallResponse changeNick(Long userId, String nick) {
        ManagerMallUser user = new ManagerMallUser();
        user.setMallUserId(userId);
        user.setNick(nick);
        managerMallUserMapper.updateByPrimaryKeySelective(user);
        return new MallResponse().success();
    }

    /**
     * @description 修改密码
     * @author 郭尔
     * @date 2019/2/15 14:50
     */
    public MallResponse sendPhoneCode(String telephone) {
        phoneCodeService.sendCode(telephone, PhoneCodeType.PAYPASSOWRD.getValue());
        return new MallResponse().success();
    }
}
